<?php 
namespace Gigasweb\Wordpress\Helper;

class Post {
	static function truncate($postToTruncate, $amount, $echo=true, $wordbreak = true, $post='') {
		global $shortname;

		if ( $postToTruncate == '' ) global $postToTruncate;

		$postExcerpt = '';
		$postExcerpt = $postToTruncate->post_excerpt;

		if (get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') {
			if ($echo) echo $postExcerpt;
			else return $postExcerpt;
		} else {
			$truncate = $postToTruncate->post_content;

			$truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate);

			if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
			$truncate = apply_filters('the_content', $truncate);
			$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate);
			$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate);

			$truncate = strip_tags($truncate);

			if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
			else $truncate = substr($truncate, 0, $amount);

			if( !$wordbreak )
				$truncate = substr( $truncate, 0, strripos( $truncate, ' '  ) );

			if ($echo) echo $truncate,$echo_out;
			else return ($truncate . $echo_out);
		};
	}
}